/*
 * 百度在线网络技术（北京）有限公司拥有本软件版权2021并保留所有权利。
 * Copyright 2021, Baidu.com,Inc 2:Baidu Online Network Technology (Beijing) Co.,Ltd,
 * All rights reserved.
 */

package com.azdebugit.mianshi.test;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description: 有2元、5元、10元三种面值的硬币，求找零N元，最少要找多少张零钱。
 * @Param: @param null
 * @return:
 * @Author: itdebug
 * @Date: 2021/7/4
 */
public class JavaTestMain {
    public static void main(String[] args) {
        Map mapList = getMapList(12);
        List integers = new ArrayList<>();
        Iterator iterator = mapList.keySet().iterator();
        while(iterator.hasNext()){
            Object next = iterator.next();
//            integers.add(Integer.valueOf(next.toString()));
            System.out.println(next.toString());
            System.out.println("----：" + mapList.get(next));
        }
    }

    public static Map getMapList(int target) {


//        Map map2 = new ConcurrentHashMap();
//        map2.Entry.put
//        Map map5 = new ConcurrentHashMap();

        return diGuiMap(target);
    }
    public static Map initMap(){
        Map map1 = new HashMap();
        map1.put(2, 0);
        map1.put(5, 0);
        map1.put(10, 0);
        return map1;
    }

    public static Map diGuiMap(int target) {
        Map map1 = new HashMap();
        Map map2 = initMap();
        Map map5 = initMap();
        Map map10 = initMap();
        Map map25 = initMap();
        Map map510 = initMap();
        Map map210 = initMap();
        Map map2510 = initMap();
       /* int v2 = Integer.valueOf(map.get(2).toString());
        int v5 = Integer.valueOf(map.get(5).toString());
        int v10 = Integer.valueOf(map.get(10).toString());
        int tempTarget = 2 * v2 + 5 * v5 + 10 * v10;*/
        //有多种凑钱方案，
        // 单独用2， tempTarget2
        int v2Total = 0;
        while (!isDaYuTarget(map2,new Integer[]{2},target)){
            map1.clear();
            v2Total = countV(map2);//Integer.valueOf(map2.get(2).toString());
            String key = "2-"+v2Total;
            map1.put(key,map2);
        }
        // 单独用5，tempTarget5
        int v5Total = 0;
        while (!isDaYuTarget(map5,new Integer[]{5},target)){
            map1.clear();
            v5Total = countV(map5);//Integer.valueOf(map5.get(5).toString());
            String key = "5-"+v5Total;
            map1.put(key,map5);
        }
        // 单独用10，tempTarget10
        int v10Total = 0;
        while (!isDaYuTarget(map10,new Integer[]{10},target)){
            map1.clear();
            v10Total = countV(map10);//Integer.valueOf(map10.get(10).toString());
            String key = "10-"+v10Total;
            map1.put(key,map10);
        }
        // 2 5，tempTarget25
        int v25Total = 0;
        while (isDaYuTarget(map25,new Integer[]{2,5},target)){
            map25.clear();
            v25Total = countV(map25);//Integer.valueOf(map25.get(2).toString()) + Integer.valueOf(map25.get(5).toString()) ;
            String key = "25-"+v25Total;
            map1.put(key,map25);
        }
        // 5 10， tempTarget510
        int v510Total = 0;
        while (isDaYuTarget(map510,new Integer[]{5,10},target)){
            map510.clear();
            v510Total = countV(map510);//Integer.valueOf(map510.get(10).toString()) + Integer.valueOf(map510.get(5).toString()) ;
            String key = "510-"+v510Total;
            map1.put(key,map510);
        }
        // 2，10  tempTarget210
        int v210Total = 0;
        while (isDaYuTarget(map210,new Integer[]{2,10},target)){
            map210.clear();
            v210Total = countV(map210);
            String key = "210-"+v210Total;
            map1.put(key,map210);
        }
        // 2，5，10  tempTarget2510
        int v2510Total = 0;
        while (isDaYuTarget(map2510,new Integer[]{2,5,10},target)){
            map2510.clear();
            v2510Total = countV(map2510);
            String key = "2510-"+v2510Total;
            map1.put(key,map2510);
        }

        return map1;
    }

    public static boolean isDaYuTarget(Map map, Integer args[], int target) {

        boolean flag = false;
        if (args.length == 1) {
            if (args[0] == 2) {
//                return target>2*Integer.valueOf(map.get(2).toString()) ? true : false;
                Integer v2 = Integer.valueOf(map.get(2).toString());
                map.put(2, v2+1);
                flag = countTotal(map)>target? true:false;
            }else if (args[0] == 5) {
                Integer v5 = Integer.valueOf(map.get(5).toString());
                map.put(5, v5+1);
                flag = countTotal(map)>target? true:false;
            }else if (args[0] == 10) {
//                return target>2*Integer.valueOf(map.get(2).toString()) ? true : false;
                Integer v10 = Integer.valueOf(map.get(10).toString());
                map.put(10, v10+1);
                flag = countTotal(map)>target? true:false;
            }
        }else if(args.length == 2){
            if (args[0] == 2&&args[1] == 5) {
//                return target>2*Integer.valueOf(map.get(2).toString()) ? true : false;
                Integer v2 = Integer.valueOf(map.get(2).toString());
                Integer v5 = Integer.valueOf(map.get(5).toString());
                map.put(2, v2+1);
                map.put(5, v5+1);
                flag = countTotal(map)>target? true:false;
            }else if (args[0] == 2&&args[1] == 10) {
//                return target>2*Integer.valueOf(map.get(2).toString()) ? true : false;
                Integer v2 = Integer.valueOf(map.get(2).toString());
                Integer v10 = Integer.valueOf(map.get(10).toString());
                map.put(2, v2+1);
                map.put(10, v10+1);
                flag = countTotal(map)>target? true:false;
            }else if (args[0] == 5&&args[1] == 10) {
//                return target>2*Integer.valueOf(map.get(2).toString()) ? true : false;
                Integer v5 = Integer.valueOf(map.get(5).toString());
                Integer v10 = Integer.valueOf(map.get(10).toString());
                map.put(5, v5+1);
                map.put(10, v10+1);
                flag = countTotal(map)>target? true:false;
            }
        }else {
            Integer v2 = Integer.valueOf(map.get(2).toString());
            Integer v5 = Integer.valueOf(map.get(5).toString());
            Integer v10 = Integer.valueOf(map.get(10).toString());
            flag =  countTotal(map)>target? true:false;
        }

        return flag;
    }
    public static Integer countTotal(Map map){
        int v2 = Integer.valueOf(map.get(2).toString());
        int v5 = Integer.valueOf(map.get(5).toString());
        int v10 = Integer.valueOf(map.get(10).toString());
        return 2*v2 + 5*v5 + 10*v10;

    }
    public static Integer countV(Map map){
        int v2 = Integer.valueOf(map.get(2).toString());
        int v5 = Integer.valueOf(map.get(5).toString());
        int v10 = Integer.valueOf(map.get(10).toString());
        return v2+v5+v10;

    }

}
